<?php

namespace App\Kuafu\Guanyuan\Service;

use App\Library\FeiShu\SendFs;

/**
 * 观远推送消息
 *
 * Class OrderPayService
 * @package App\Kuafu\Order\Service
 */
class GuanyuanNotify
{
    /**
     * Send FS Report
     *
     * @param $exception
     */
    private static function sendFSReport($exception)
    {
        // 报错信息完善内容
        $error_message_func = function() use ($exception) {
            $baseMessage = $exception->getMessage();

            if ($exception->getPrevious()) {
                $baseMessage .= PHP_EOL."-----".PHP_EOL.$exception->getPrevious()->getMessage();
            }

            return $baseMessage;
        };

        $message = "[" . config('app.name') . "] の【" . config('app.env') . "】 环境出错啦 : \n\n\r";

        $request_url    = \request()->getUri();
        $request_method = request()->method();
        $request_params = json_encode(\request()->all());
        $request_header = json_encode(\request()->header());
        $server_ip      = $_SERVER['SERVER_ADDR'] ?? '0.0.0.0';
        $error_line     = $exception->getLine();
        $error_message  = $error_message_func();
        $error_file     = $exception->getFile();

        $data = compact(
            'request_method',
            'request_url',
            'error_message',
            'request_params',
            'request_header',
            'server_ip',
            'error_line',
            'error_file'
        );

        foreach ($data as $key => $datum) {
            $arr     = explode('_', $key);
            $message .= "[";
            foreach ($arr as $item) {
                $message .= trans('messages.' . $item);
            }
            $message .= "]：\n" . $datum . "\n\n";
        }

        // 机器人错误提醒
        try {
            if ('dev' != config('app.env')) {
                SendFs::newRobot()->setTitleContent('报错信息推送', $message)->send();
            }

        } catch (\Exception $e) {
        }
    }
}
